package com.site.blog.my.core.controller.admin;

import com.site.blog.my.core.entity.AdminUser;
import com.site.blog.my.core.entity.BlogCategory;
import com.site.blog.my.core.service.AdminUserService;
import com.site.blog.my.core.service.BlogCategoryService;
import com.site.blog.my.core.util.PageQueryUtil;
import com.site.blog.my.core.util.Result;
import com.site.blog.my.core.util.ResultGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @ClassName: my-blog
 * @description:
 * @author: zxf
 * @create: 2021-06-30 12:45
 **/
@Controller
@RequestMapping("/admin")
public class CategoryController {

    @Resource
    private BlogCategoryService blogCategoryService;


    @GetMapping("/categories")
    public String categoryPage(HttpServletRequest request) {
        request.setAttribute("path", "categories");
        return "admin/category";
    }
    @GetMapping("/categories/list")
    @ResponseBody
    public Result list(@RequestParam Map<String,Object> params){

        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))){
            return ResultGenerator.genFailResult("参数错误~");
        }

        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        // 数据层查出分页数据，业务层再包装一下PageResult到date中返回给前端
        return ResultGenerator.genSuccessResult(blogCategoryService.getBlogCategoryPage(pageQueryUtil));
    }

    @PostMapping("/categories/save")
    @ResponseBody
    public Result save(@RequestParam(value = "categoryName") String categoryName,
                       @RequestParam(value = "categoryIcon") String categoryIcon){
        if (StringUtils.isEmpty(categoryName)){
            return ResultGenerator.genFailResult("请输入分类名~");
        }
        if (StringUtils.isEmpty(categoryIcon)){
            return ResultGenerator.genFailResult("请输入分类图标~");
        }
        if (blogCategoryService.saveCategory(categoryName, categoryIcon)){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult("分类名重复~");
        }
    }
    @PostMapping("/categories/update")
    @ResponseBody
    public Result update(@RequestParam(value = "categoryId") Integer categoryId,
                         @RequestParam(value = "categoryName") String categoryName,
                         @RequestParam(value = "categoryIcon") String categoryIcon){
        if (categoryId == null || categoryId < 1) {
            return ResultGenerator.genFailResult("非法参数~");
        }
        if (StringUtils.isEmpty(categoryName)){
            return ResultGenerator.genFailResult("请输入分类名~");
        }
        if (StringUtils.isEmpty(categoryIcon)){
            return ResultGenerator.genFailResult("请输入分类图标~");
        }
        if (blogCategoryService.updateCategory(categoryId,categoryName,categoryIcon)){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult("分类名重复~");
        }
    }
    @GetMapping("/categories/info/{id}")
    @ResponseBody
    public Result info(@PathVariable Integer id){
        if (id == null || id < 1) return ResultGenerator.genFailResult("非法参数~");
        BlogCategory blogCategory = blogCategoryService.selectById(id);
        return ResultGenerator.genSuccessResult(blogCategory);
    }

    @PostMapping("/categories/delete")
    @ResponseBody
    public Result delete(@RequestBody Integer[] index){
        if (index == null || index.length < 1) return ResultGenerator.genFailResult("参数异常~");
        if (blogCategoryService.deleteBatch(index)){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult("删除失败~");
        }

    }

}

